﻿using CKS.Core.Framework;
using Microsoft.Extensions.DependencyInjection;
using Infrastructure;

using Mapster;

namespace CKS.PLC.Action.OP10
{
    /// <summary>
    /// 动作编号1 ，2 处理错误
    /// 入口错误
    /// 使用次数达到报警
    /// 主界面需要展示错误项，收到重置状态后，移除错误项
    /// 
    /// </summary>
    public class Cks01Action : PLCAction
    {
        public Cks01Action(DecodeValue _value) : base(_value)
        {
        }
        
        public override void Execute()
        {
            // EventBus.ErrorStateChange(this ,Value); InternalApp.JobProvider

            var job = InternalApp.JobProvider.GetService<PushJob>();

            string key = ""; // 报错位置
            if (Value.Position.ActionId == 1)
            {
                var error = Value.Value.ToInt();
                if (error > 0)
                {
                    string location = Value.Position.DataId switch
                    {
                        1 => "L1入口",
                        2 => "L2入口",
                        3 => "L3入口",
                        4 => "L4入口",
                        _ => ""
                    };
                    string errorMsg = error switch
                    {
                        1 => "子篮层数错误",
                        2 => "子篮扫码失败",
                        3 => "子篮扫码超时",
                        4 => "子篮读零件号与线体不一致",
                        5 => "子篮数据保存失败",
                        6 => "子篮数据保存超时",
                        _ => ""
                    };
                    if(error>0 && errorMsg.IsEmpty())
                    {
                        errorMsg = GetLoadingLineErrormessage(Value.Position.DataId,error);
                    }

                    string message = $"PLC错误: {location},{errorMsg}";

                    job.AddDeviceAlarm($"1_{Value.Position.DataId}", message);
                }
                else
                {
                    job.RemoveDeviceAlarm($"1_{Value.Position.DataId}");
                }

            }
            else if (Value.Position.ActionId == 2)
            {
                string product = Value.Position.DataId switch
                {
                    1 => "翅片QXA",
                    2 => "盖板QXK",
                    3 => "底板QXM",
                    4 => "水嘴焊环大QXW",
                    5 => "定位柱焊环小QXZ",
                    6 => "焊丝QXX",
                    7 => "焊片QXY",
                    8 => "中板QXH",
                    9 => "接头QXN",
                   10 => "定位柱QXG",
                    _ => ""
                };

                //遍历数组
                var items = Value.Value.Adapt<List<int>>();
                for (int i = 0; i < items.Count; i++)
                {
                    var item = items[i];
                    var itemno = i + 1;
                    string message = $"PLC错误: {product},子篮编号{itemno},使用次数不足！";

                    key = $"2_{Value.Position.DataId}_{itemno}";
                    if (item > 0)
                    {
                        //ShowErrorForm(key, $"{product}使用次数不足,子篮编号{itemno}", message);
                        job.AddAgeAlarm(key, message);
                    }
                    else
                    {
                        job.RemoveAgeAlarm(key);
                    }
                }


            }


        }
        /*
         *  //=======上料L1输送入口报警代码定义发送给MES=======
(*
1=子篮层数错误;
2=子篮扫码失败;
3=子篮扫码超时;
4=子篮读码零件号与线体号不一致;
5=子篮零件无需生产
6=子篮数据保存超时
7=【L1上料提示】L1输送入口无需零件
8=【L1上料提示】L1输送入口提示上翅片QXA
*)
//=======上料L2输送入口报警代码定义发送给MES=======
(*1=子篮层数错误;
2=
子篮扫码失败;
3=子篮扫码超时;
4=子篮读码零件号与线体号不一致;
5=子篮零件无需生产
6=子篮数据保存超时
7=【L2上料提示】L2输送入口无需零件
8=【L2上料提示】L2输送入口提示上盖板QXK
9=【L2上料提示】L2输送入口提示上底板QXM
*)
//=======上料L3输送入口报警代码定义发送给MES=======
(*
1=子篮层数错误;
2=子篮扫码失败;
3=子篮扫码超时;
4=子篮读码零件号与线体号不一致;
5=子篮零件无需生产
6=子篮数据保存超时
7=【L3上料提示】L3输送入口无需零件
8=【L3上料提示】L3输送入口提示上水嘴大焊环QXW
9=【L3上料提示】L3输送入口提示上定位柱焊环小QXZ
10=【L3上料提示】L3输送入口提示上焊丝QXX
11=【L3上料提示】L3输送入口提示上焊片QXY
*)
//=======上料L4输送入口报警代码定义发送给MES=======
(*
1=子篮层数错误;
2=子篮扫码失败;
3=子篮扫码超时;
4=子篮读码零件号与线体号不一致;
5=子篮零件无需生产
6=子篮数据保存超时
7=【L3上料提示】L3输送入口无需零件
8=【L4上料提示】L4输送入口提示上中板QXH
9=【L4上料提示】L4输送入口提示上接头QXN
10=【L4上料提示】L4输送入口提示上定位柱QXG
*)
         
         
         */
        private string GetLoadingLineErrormessage(int lineId, int error)
        {
           if(lineId == 1)
            {
                switch (error)
                {
                    case 7: return "无需零件";
                    case 8: return "提示上翅片QXA";
                }
            }
           else if(lineId ==2)
            {
                switch (error)
                {
                    case 7: return "无需零件";
                    case 8: return "提示上盖板QXK";
                    case 9: return "提示上底板QXM";
                }
            }
            else if (lineId == 3)
            {
                switch (error)
                {
                    case 7: return "无需零件";
                    case 8: return "提示上水嘴大焊环QXW";
                    case 9: return "提示上定位柱焊环小QXZ";
                    case 10: return "提示上焊丝QXX";
                    case 11: return "提示上焊片QXY";
                }
            }
            else if (lineId == 4)
            {
                switch (error)
                {
                    case 7: return "无需零件";
                    case 8: return "提示上中板QXH";
                    case 9: return "提示上接头QXN";
                    case 10: return "提示上定位柱QXG";
           
                }
            }
            return "";
        }
    }
}
